package com.zf.zfed.audit.ui

import android.content.Context
import android.util.AttributeSet
import android.view.Gravity
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.zf.zfed.audit.R

/**
 * 角色选择按钮组件
 */
class RoleButton @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
    
    private var roleIcon: ImageView
    private var roleText: TextView
    private var isSelected: Boolean = false
    
    init {
        isClickable = true
        isFocusable = true
        
        // 设置背景
        background = ContextCompat.getDrawable(context, R.drawable.role_button_background)
        
        // 设置内边距
        val padding = context.dpFromDimen(R.dimen.spacing_8)
        setPadding(padding, padding, padding, padding)
        
        // 图标
        roleIcon = ImageView(context)
        val iconSize = context.dpFromDimen(R.dimen.size_24)
        val iconLP = LayoutParams(iconSize, iconSize)
        iconLP.gravity = Gravity.START or Gravity.CENTER_VERTICAL
        iconLP.leftMargin = context.dpFromDimen(R.dimen.spacing_8)
        addView(roleIcon, iconLP)
        
        // 文字
        roleText = TextView(context)
        roleText.setTextColor(ContextCompat.getColor(context, R.color.text_primary))
        roleText.textSize = context.spFromDimen(R.dimen.font_18)
        roleText.setTypeface(null, android.graphics.Typeface.BOLD)
        val textLP = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
        textLP.gravity = Gravity.END or Gravity.CENTER_VERTICAL
        textLP.rightMargin = context.dpFromDimen(R.dimen.spacing_8)
        addView(roleText, textLP)
        
        if (attrs != null) {
            val typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoleButton)
            val iconRes = typedArray.getResourceId(R.styleable.RoleButton_role_icon, 0)
            val text = typedArray.getString(R.styleable.RoleButton_role_text)
            val selected = typedArray.getBoolean(R.styleable.RoleButton_is_selected, false)
            
            if (iconRes != 0) {
                roleIcon.setImageResource(iconRes)
                roleIcon.setColorFilter(ContextCompat.getColor(context, R.color.text_primary))
            }
            
            roleText.text = text
            setSelected(selected)
            
            typedArray.recycle()
        }
    }
    
    /**
     * 设置选中状态
     */
    override fun setSelected(selected: Boolean) {
        isSelected = selected
        updateBackground()
    }
    
    /**
     * 获取选中状态
     */
    override fun isSelected(): Boolean = isSelected
    
    /**
     * 更新背景
     */
    private fun updateBackground() {
        background = if (isSelected) {
            ContextCompat.getDrawable(context, R.drawable.role_button_selected_background)
        } else {
            ContextCompat.getDrawable(context, R.drawable.role_button_background)
        }
    }
}

